package ruletree;

import java.io.File;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JOptionPane;

import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;

import components.Component;

import ruletree.rule.Rule;

public class RuleTree {
	protected String name;
	protected ArrayList<Folder> folderList;
	protected ArrayList<Rule> ruleList;

	protected RuleTree() {
		this.folderList = new ArrayList<>();
		this.ruleList = new ArrayList<>();
	}

	protected RuleTree(Element rootNode) {
		name = rootNode.getAttributeValue("name");
		folderList = new ArrayList<>();
		ruleList = new ArrayList<>();
	}

	protected RuleTree(String name) {
		this.name = name;
		this.folderList = new ArrayList<>();
		this.ruleList = new ArrayList<>();
	}

	public String getName() {
		return name;
	}

	public void addFolder(RuleTree folder) {
		folderList.add((Folder) folder);
	}

	public void addRule(RuleTree rule) {
		ruleList.add((Rule) rule);
	}

	public static void testInstance(Element rootNode) {
		// System.out.println(rootNode);
		List<Element> children = rootNode.getChildren();
		for (Element child : children) {

			// System.out.println(child.getAttributeValue("name"));

			testInstance(child);
		}
	}

	public static RuleTree createInstance(Element rootNode, String dir) {

		if (rootNode.getName().equals("rules")) {
			SAXBuilder builder = new SAXBuilder();
			dir = dir + "\\" + rootNode.getAttributeValue("name") + ".xml";
			File file = new File(dir);
			try {
				Document doc = builder.build(file);
				Element node = doc.getRootElement();

				Rule rule = Rule.createInstance(node);
				System.out.println(rule.getName() + "RULE");
				return rule;

			} catch (Exception e) {
				e.printStackTrace();
				return null;
			}
		} else {
			List<Element> children = rootNode.getChildren();
			Folder folder = new Folder(rootNode);
			for (Element child : children) {
				if (child.getName().equals("folder")){
					System.out.println(child.getAttributeValue("name") + "FOLDER");
					folder.addFolder((Folder) createInstance(child, dir + "\\" + rootNode.getAttributeValue("name")));
				}
				else
					folder.addRule((Rule) createInstance(child, dir + "\\" + rootNode.getAttributeValue("name")));
			}
			
			System.out.println("Before Return folder contains " + folder.getFolderList());
			return folder;
		}

	}

	public ArrayList<Folder> getFolderList() {
		return folderList;
	}

	public ArrayList<Rule> getRuleList() {
		return ruleList;
	}

	public Element generateXMLRuleTree(RuleTree rootNode) {

		System.out.println("rootNode is" + rootNode.name);
		//System.out.println(rootNode.folderList != null);
		System.out.println("rootNode size" + rootNode.folderList.size());
		
		System.out.println(rootNode.folderList.size() > 0);
		
		Element n = new Element(rootNode.name);
		n.setAttribute("name", rootNode.name);
		
		if (rootNode.folderList.size() > 0) {
			Element element = new Element("folder");
			element.setAttribute("name", name);
			System.out.println("folderList contains" + rootNode.folderList.size());
			for (Folder folder : rootNode.folderList) {
				System.out.println("entered");
				element.addContent(generateXMLRuleTree(folder));
				System.out.println("dfgdfgf");
			}
			n.addContent(element);
		}
		System.out.println("bksbxksxbksj");
		
		if (rootNode.ruleList.size() > 0) {
			Element folder = new Element("folder");
			folder.setAttribute("name", rootNode.name);
			for (Rule rule : ruleList) {
				System.out.println("Save " + rule.name);
				folder.addContent(rule.generateXMLRule(folder));
			}
			n.addContent(folder);
		}

		return n;
	}

	public void saveXML(Element xmlElement) {

		File f = new File("Languages\\Filipino\\XMLRules\\tree.out.xml");

		XMLOutputter xmlOutput = new XMLOutputter();
		xmlOutput.setFormat(Format.getPrettyFormat());

		try {
			xmlOutput.output(xmlElement, new FileWriter(f));
			JOptionPane.showMessageDialog(null,
					"Successfully Saved '" + f.getName() + "'",
					"Save Success!", JOptionPane.INFORMATION_MESSAGE);
		} catch (Exception e) {
			JOptionPane.showMessageDialog(null, "Error in saving file!",
					"Save Error", JOptionPane.ERROR_MESSAGE);
		}
	}
}